package cn.zh.npro.Fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.npro.Activity.ORorder;
import cn.zh.npro.Activity.OrDetail;
import cn.zh.npro.Adapter.OrAdapter;
import cn.zh.npro.Item.OrItem;
import cn.zh.npro.MyApplication;
import cn.zh.npro.R;
import cn.zh.npro.Util.BaseUrl;
import cn.zh.npro.Util.Loading;
import cn.zh.npro.Util.MyListener;
import cn.zh.npro.Util.PullToRefreshLayout;
import cn.zh.npro.Util.PullableListView;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by WYN on 2018/2/26 0026.
 */

public class OrderFragment extends Fragment implements PullableListView.OnLoadListener {

    PullableListView pulist;
    PullToRefreshLayout refresh_view;
    TextView empty;
    int page=1;
    String uid;
    String cid="";
    MyApplication mapp;
    ArrayList<OrItem> orItems=new ArrayList<OrItem>();
    OrAdapter orAdapter=null;
    ImageView adtrade;
    public static final int INIT = 0;
    public static final int STOP = 2;
    private Dialog LDialog;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.or_layout,container,false);
        mapp= (MyApplication) getActivity().getApplication();
        uid=mapp.getSp().getString("uid","");
        initof(view);
        getOrder(page+"");
        return view;
        }

     public void initof(View view){
         refresh_view=(PullToRefreshLayout)view.findViewById(R.id.orefresh_view);
         empty= (TextView)view.findViewById(R.id.ofempty);
         pulist= (PullableListView)view.findViewById(R.id.ofpulist);
         adtrade= (ImageView)view.findViewById(R.id.adtrade);
         pulist.setOnLoadListener(this);
         orAdapter=new OrAdapter(getActivity(),orItems,mapp);
         pulist.setAdapter(orAdapter);
         pulist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             @Override
             public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                 Intent intent=new Intent(getActivity(), OrDetail.class);
                 intent.putExtra("object",orItems.get(i));
                 startActivity(intent);
              }
             });


         refresh_view.setOnRefreshListener(new MyListener(){
             @Override
             public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                 super.onRefresh(pullToRefreshLayout);
                 //下拉刷新
                 page=1;
                 getOrder(page+"");
                 }
              });


         adtrade.setOnClickListener(new View.OnClickListener(){
             @Override
             public void onClick(View view) {
             Intent intent=new Intent(getActivity(),ORorder.class);
             startActivity(intent);
             }
           });
          }


    @Override
    public void onLoad(PullableListView pullableListView) {
         page=page+1;
         getOrder(page+"");
         }



         public void getOrder(final String page){
             if (page.equals("1")){
                 LDialog= Loading.createLoadingDialog(getActivity(),"加载中...");
               }
             OkHttpUtils.get(BaseUrl.GetTradeList)
                     .params("userId",uid)
                     .params("customerId",cid)
                     .params("pageIndex",page)
                     .params("pageSize","5")
                     .execute(new StringCallback() {
                         @Override
                         public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                             if (null!=LDialog){
                                 Loading.closeDialog(LDialog);
                               }
                             try {
                                 JSONObject js=new JSONObject(s);
                                 boolean State=js.getBoolean("State");
                                 if (State){
                                     JSONArray Res=js.getJSONArray("Res");
                                     if (page.equals("1")){
                                         orItems.clear();
                                         if (0==Res.length()){
                                             empty.setVisibility(View.VISIBLE);
                                             pulist.setVisibility(View.GONE);
                                             pulist.finishLoading(INIT);
                                             return;
                                         }else{
                                             empty.setVisibility(View.GONE);
                                             pulist.setVisibility(View.VISIBLE);
                                              }
                                            }else {
                                         if (Res.length()==0){
                                             pulist.finishLoading(STOP);
                                             return;
                                           }
                                         }
                                     for (int i=0;i<Res.length();i++){
                                         JSONObject ji=Res.getJSONObject(i);
                                         String TradeID=ji.getString("TradeID");
                                         String TradeTime=ji.getString("TradeTime");
                                         String TradeStatus=ji.getString("TradeStatus");
                                         String TradeNO=ji.getString("TradeNO");
                                         String CustomerName=ji.getString("CustomerName");
                                         double GoodsTotal=ji.getDouble("GoodsTotal");
                                         OrItem orItem=new OrItem(TradeID,TradeTime,TradeStatus,TradeNO,CustomerName,GoodsTotal);
                                         orItems.add(orItem);
                                         }
                                     orAdapter.notifyDataSetChanged();
                                     }
                                 pulist.finishLoading(INIT);
                              } catch (JSONException e) {
                                 e.printStackTrace();
                               }
                             }
                         @Override
                         public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                             super.onError(isFromCache, call, response, e);
                         }
                     });
                  }






    }
